package com.lfy.chat.controller;

import com.lfy.auth.domain.entity.UserEntity;
import com.lfy.chat.domain.bo.FriendBO;
import com.lfy.chat.domain.request.AgreeFriendReq;
import com.lfy.chat.domain.request.NoAgreeFriendReq;
import com.lfy.chat.domain.request.addFriendReq;
import com.lfy.chat.domain.request.SingleMsgReq;
import com.lfy.chat.domain.response.*;
import com.lfy.chat.service.ChatService;
import com.lfy.common.result.R;
import com.lfy.commons.anno.chat.AddFriendVerification;
import com.lfy.commons.anno.chat.FriendVerification;
import com.lfy.commons.anno.limit.CurrentLimit;
import com.lfy.commons.anno.user.AutoUser;
import com.lfy.commons.strategy.LimitStrategyEnum;
import com.lfy.commons.utils.JwtUtil;
import com.lfy.commons.utils.RedisUtil;
import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/chat")
public class ChatController {
    @Resource
    private ChatService chatService;
    /**
     * 单聊
     * @return R
     */
    @PostMapping("/single/send")
    @FriendVerification(el="#msgReq.to")
    @CurrentLimit(time = 10,once = 20,nuit = TimeUnit.SECONDS,strategy = LimitStrategyEnum.Id)
    @CurrentLimit(time = 1,once = 60,nuit = TimeUnit.MINUTES,strategy = LimitStrategyEnum.Id)
    @CurrentLimit(time = 1,once = 2000,nuit = TimeUnit.HOURS,strategy = LimitStrategyEnum.Id)
    public R<Void> singleSendMsg(@AutoUser UserEntity user, @Valid @RequestBody SingleMsgReq msgReq){

        return chatService.singleSend(msgReq,user);
    }

    /**
     * 获取离线聊天消息
     * @param user 用户
     * @return R
     */
    @GetMapping("/single/getOfflineMsg")
    @CurrentLimit(time = 1,once = 15,nuit = TimeUnit.MINUTES,strategy = LimitStrategyEnum.Id)
    @Deprecated
    public R<List<OfflineListResp>> getOfflineMsg(@AutoUser UserEntity user){
        return chatService.getOfflineMsg(user);
    }

    /**
     * 获取离线好友申请
     * @param user 用户
     * @return R
     */
    @GetMapping("/getOfflineFriendMsg")
    @CurrentLimit(time = 1,once = 15,nuit = TimeUnit.MINUTES,strategy = LimitStrategyEnum.Id)
    public R<List<OfflineFriendResp>> getOfflineFriendMsg(@AutoUser UserEntity user){
        return chatService.getOfflineFriendMsg(user);
    }
    /**
     * 添加好友
     * @param user 用户
     * @param req req
     * @return R
     */
    @PostMapping("/addFriend")
    @CurrentLimit(time = 1,once = 15,nuit = TimeUnit.MINUTES,strategy = LimitStrategyEnum.Id)
    @CurrentLimit(time = 1,once = 100,nuit = TimeUnit.DAYS,strategy = LimitStrategyEnum.Id)
    @AddFriendVerification(el = "#req.to")
    public R<Void> addFriend(@AutoUser UserEntity user,@Valid @RequestBody addFriendReq req){
        return chatService.addFriend(user,req);
    }
    /**
     * 同意好友申请
     */
    @PostMapping("/agreeFriend")
    public R<Void> agreeFriend(@AutoUser UserEntity user,@Valid @RequestBody AgreeFriendReq agreeFriendReq){
        return chatService.agreeFriend(user,agreeFriendReq);
    }
    /**
     * 拒绝好友申请
     */
    @PostMapping("/noAgreeFriend")
    public R<Void> noAgreeFriend(@AutoUser UserEntity user, @Valid @RequestBody NoAgreeFriendReq noAgreeFriendReq){
        return chatService.noAgreeFriend(user,noAgreeFriendReq);
    }
    /**
     * 获取对话列表
     */
    @GetMapping("/getNowChatList")
    public R<List<FriendBO>> getChatNowFriend(@AutoUser UserEntity user){
        return chatService.getChatNowFriendList(user);
    }
    /**
     * 从对话列表中删除
     */
    @GetMapping("/removeNowChat")
    public R<Void> removeNowChat(@AutoUser UserEntity user,@RequestParam("id") String id){
        return chatService.removeNotChat(user,id);
    }
    /**
     * 添加对话列表
     */
    @GetMapping("/addNowChat")
    @FriendVerification(el="#id")
    public R<Void> addFriendNowChat(@AutoUser UserEntity user,@RequestParam("id") String id){
        return chatService.addFriendNowChat(user,id);

    }

    /**
     * 获取验证消息列表
     */
    @GetMapping("/getVerifyMsg")
    @CurrentLimit(time = 60)
    public R<List<VerifyMsgResp>> getVerifyMsg(@AutoUser UserEntity entity){
        return chatService.getVerifyMsgList(entity);
    }

    /**
     * 获取好友列表
     * @param user 用户
     */
    @GetMapping("/getFriends")
    @CurrentLimit(time = 60,once = 20,strategy = LimitStrategyEnum.Id)
    public R<List<UserFriendResp>> getFriendList(@AutoUser UserEntity user){
        return chatService.getFriendList(user);
    }

    @GetMapping("/del/friend")
    public R<Void> delFriend(@AutoUser UserEntity user,@RequestParam("id")String id){
        return chatService.delFriend(user,id);
    }
}

